import http from "@/utils/http";
import { bodyToQuery } from "@/utils/helper";
import type { 
    StockInAddType, 
    StockInSearchType, 
    StockInUpdateStatusData, 
    StockInDetailParams 
} from "./params";
import type { 
    StockInListData, 
    StockInAddData, 
    StockInDetailData, 
    StockInPagedListData 
} from "./res";
import { headerCommon, headerMaster } from "../header";

// API路径枚举
enum API {
    GET_STOCK_IN_LIST_API = "/1.1/classes/buy",          // 入库单列表查询
    POST_STOCK_IN_ADD_API = "/1.1/classes/buy",         // 新增入库单
    PUT_STOCK_IN_UPDATE_API = "/1.1/classes/buy",       // 更新入库单
    GET_STOCK_IN_DETAIL_API = "/1.1/classes/buy",       // 入库单详情
    //删除入库单
    PUT_STOCK_IN_DELETE_API = "/1.1/classes/buy",

    // GET_STOCK_IN_PAGE_API = "/1.1/classes/buy"   // 入库单分页查询
}

/**
 * 获取入库单列表
 * @param params 查询参数（可选）
 */
export const getStockInListApi = (params?: StockInSearchType) =>
    http.get<any, StockInListData>(
        API.GET_STOCK_IN_LIST_API + `${params ? `?where=${JSON.stringify(params)}` : ""}`,
        { headers: headerCommon }
    );

/**
 * 新增入库单
 * @param data 入库单数据
 */
export const postStockInAddApi = (data: StockInAddType) =>
    http.post<any, StockInAddData>(
        API.POST_STOCK_IN_ADD_API, 
        data, 
        { headers: headerMaster }
    );

/**
 * 删除入库单
 */
export const putStockInDeleteApi = (objectId: string) =>
    http.delete<any, any>(
        `${API.PUT_STOCK_IN_DELETE_API}/${objectId}`,
        { headers: headerMaster }
    );

/**
 * 获取入库单详情
 *  包含objectId
 */
export const getStockInDetailApi = (objectId: string) =>
    http.get<any, StockInDetailData>(
        `${API.GET_STOCK_IN_DETAIL_API}/${objectId}`,
        { headers: headerCommon }
    );


/**
 * 更新入库单状态
 * @param data 包含objectId和status
 */
export const putStockInUpdateStatusApi = (data: StockInUpdateStatusData) =>
    http.put<any, any>(
        `${API.PUT_STOCK_IN_UPDATE_API}/${data.objectId}`,
        { status: data.status, ...(data.remark && { remark: data.remark }) },
        { headers: headerMaster }
    );



/**
 * 入库单分页查询
 * @param params 分页及查询参数
 */
// export const getStockInPageApi = (params: StockInPageParams) => {
//     const query = {
//         page: params.page || 1,
//         size: params.size || 10,
//         ...(params.sortField && { sort: `${params.sortField}|${params.sortOrder || 'asc'}` }),
//         ...params.queryParams
//     };
//     return http.get<any, StockInPagedListData>(
//         `${API.GET_STOCK_IN_PAGE_API}?${bodyToQuery(query)}`,
//         { headers: headerCommon }
//     );
// };